package com.example.wangyi;

import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.FrameLayout;
import android.widget.Space;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import com.example.wangyi.base.BaseActivity;
import com.example.wangyi.base.BasePresenter;
import com.example.wangyi.mvp.ui.fragment.classify.ClassifyFragment;
import com.example.wangyi.mvp.ui.fragment.home.HomeFragment;
import com.example.wangyi.mvp.ui.fragment.my.MyFragment;
import com.example.wangyi.mvp.ui.fragment.shopping.ShoppingFragment;
import com.example.wangyi.mvp.ui.fragment.special.SpecialFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends BaseActivity {


    @BindView(R.id.fl)
    FrameLayout fl;
    @BindView(R.id.main_bnv)
    BottomNavigationView mainBnv;
    private HomeFragment homeFragment;
    private SpecialFragment specialFragment;
    private ClassifyFragment classifyFragment;
    private ShoppingFragment shoppingFragment;
    private MyFragment myFragment;
    private Fragment[] fragments;
    private int lastFragment;

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }

    @Override
    protected void init() {
        homeFragment = new HomeFragment();
        specialFragment = new SpecialFragment();
        classifyFragment = new ClassifyFragment();
        shoppingFragment = new ShoppingFragment();
        myFragment = new MyFragment();

        fragments = new Fragment[]{homeFragment, specialFragment, classifyFragment, shoppingFragment, myFragment};

        getSupportFragmentManager().beginTransaction().add(R.id.fl, homeFragment).show(homeFragment).commit();
        lastFragment = 0;

        mainBnv.setOnNavigationItemSelectedListener(changeFragment);
    }

    private BottomNavigationView.OnNavigationItemSelectedListener changeFragment = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
            switch (menuItem.getItemId()){
                case R.id.menu1:
                    if(lastFragment!=0){
                        switchFragment(lastFragment,0);
                        lastFragment=0;
                    }
                    return true;
                case R.id.menu2:
                    if(lastFragment!=1){
                        switchFragment(lastFragment,1);
                        lastFragment=1;
                    }
                    return true;
                case R.id.menu3:
                    if(lastFragment!=2){
                        switchFragment(lastFragment,2);
                        lastFragment=2;
                    }
                    return true;
                case R.id.menu4:
                    if(lastFragment!=3){
                        switchFragment(lastFragment,3);
                        lastFragment=3;
                    }
                    return true;
                case R.id.menu5:
                    if(lastFragment!=4){
                        switchFragment(lastFragment,4);
                        lastFragment=4;
                    }
                    return true;

            }
            return false;
        }
    };

    @Override
    protected void onResume() {
        Log. e( "zz", "onResume");
        int id= getIntent().getIntExtra("id",  0);
        if(id == 1){
            switchFragment(lastFragment,3);
            lastFragment=3;
        }
        super.onResume();
    }


    private void switchFragment(int lastfragment, int index) {
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.hide(fragments[lastfragment]);//隐藏上个Fragment

        if (fragments[index].isAdded() == false) {
            transaction.add(R.id.fl, fragments[index]);
        }
        transaction.show(fragments[index]).commitAllowingStateLoss();

    }


    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_main;
    }


}